// 从一个数组中对象过滤出另一个数组对应的键的数据
type IFilter = 'excludes' | 'includes'
export function filterFromArray(
  arr: any[],
  filterArr: string[],
  flied: string = 'value',
  type: IFilter = 'includes',
) {
  return arr.filter((item) => {
    return filterArr.find((v) => {
      if (type === 'excludes')
        return v !== item[flied]
      return v === item[flied]
    })
  })
}
/**
 * 从第二个数组中排除指定数组中的元素
 *
 * @param arr 主数组，包含多个对象，每个对象有 value 和 operatorOptions 属性
 * @param filterArr 需要从 operatorOptions 中排除的字符串数组
 * @param field 用于比较 filterArr 中字符串的字段名，默认为 'value'
 * @returns 返回过滤后的主数组
 */
export function excludeFromSecondArray(
  arr: any[],
  filterArr: string[],
  field: string = 'value',
) {
  return arr.filter((item) => {
    if (item.value !== 'ip')
      return item
    let data = []
    data = item.operatorOptions.filter((val) => {
      return !filterArr.includes(val[field])
    })
    item.operatorOptions = data
    return data
  })
}
// 从对象数组中取出一个键名的值组成新数组
export function getFieldsToArr(arr, field) {
  return arr.map((o) => {
    return o[field]
  })
}
// 过滤掉l2
export function filterTree(tree: any, arr: any = []) {
  if (!tree.length)
    return []
  for (const item of tree) {
    // if (item.cascade_level && item.cascade_level !== 1) continue //  过滤掉 l2
    const node = { ...item, nodes: [] }
    arr.push(node)
    if (item.nodes && item.nodes.length) {
      filterTree(item.nodes, node.nodes)
    }
  }
  return arr
}
export function transformToTree(input: any[]) {
  const result: Node[] = []
  for (const item of input) {
    const node: any = {
      id: item.id,
      name: item.name,
      type: item.type,
      children: item.list?.length
        ? item.list
            .map((nodeItem: any) => ({
              id: nodeItem.id,
              // ip: nodeItem.ips?.length
              //   ? nodeItem.ips[0]
              //   : nodeItem.ips?.toString(),
              ip: nodeItem.ip,
              // name: `${
              //   nodeItem.name
              // }（${nodeItem.ips?.length ? nodeItem.ips[0] : nodeItem.ips?.toString()}）`,
              name: `${nodeItem.name}（${nodeItem.ip}）`,
              // level: nodeItem.cascade_level,
            }))
            .sort((a: any, b: any) => a.level - b.level)
        : [],
    }
    if (node.children && node.children?.length > 0) {
      result.push(node)
    }
  }
  return result
}
